function [capacity_tdma, delay_analysis, throughput, fig_tdma] = tdma_analysis(EbN0_dB, num_users, frame_structures)
% TDMA多址接入技术分析
% 输入参数：
%   EbN0_dB - 信噪比范围 (dB)
%   num_users - 用户数量数组
%   frame_structures - 帧结构参数 (时隙数量)
% 输出参数：
%   capacity_tdma - TDMA容量
%   delay_analysis - 延迟分析结果
%   throughput - 吞吐量分析
%   fig_tdma - 图形句柄

% 添加路径
addpath('../Common');
colors = color_definitions();

% 参数设置
slot_duration = 1e-3; % 时隙持续时间 (秒)
frame_duration = 10e-3; % 帧持续时间 (秒)
channel_gains = 1; % 信道增益
noise_power_dBm = -174 + 10*log10(180e3); % 噪声功率
noise_power = 10^((noise_power_dBm-30)/10);

% 初始化结果数组
num_snr = length(EbN0_dB);
num_user_cases = length(num_users);
num_frame_cases = length(frame_structures);
capacity_tdma = zeros(num_snr, num_user_cases);
delay_analysis = zeros(num_user_cases, num_frame_cases);
throughput = zeros(num_snr, num_user_cases);

fprintf('TDMA多址接入技术分析...\n');

% TDMA容量和性能分析
for snr_idx = 1:num_snr
    snr_linear = 10^(EbN0_dB(snr_idx)/10);
    
    for user_idx = 1:num_user_cases
        K = num_users(user_idx);
        
        % 每个用户的时隙数量
        slots_per_frame = frame_structures(min(user_idx, num_frame_cases));
        slots_per_user = floor(slots_per_frame / K);
        
        % 每用户有效时间比例
        time_fraction = slots_per_user / slots_per_frame;
        
        % 计算容量 (使用完整SNR，但时间受限)
        user_capacity = time_fraction * log2(1 + snr_linear * channel_gains);
        
        % 总容量
        capacity_tdma(snr_idx, user_idx) = K * user_capacity;
        
        % 吞吐量 (考虑开销)
        overhead_ratio = 0.1; % 10%开销
        throughput(snr_idx, user_idx) = capacity_tdma(snr_idx, user_idx) * (1 - overhead_ratio);
    end
end

% 延迟分析
for user_idx = 1:num_user_cases
    K = num_users(user_idx);
    
    for frame_idx = 1:num_frame_cases
        slots_per_frame = frame_structures(frame_idx);
        slots_per_user = floor(slots_per_frame / K);
        
        % 等待时间分析
        if slots_per_user > 0
            % 平均等待时间 (假设均匀分布)
            avg_waiting_time = (slots_per_frame - slots_per_user) * slot_duration / 2;
            max_waiting_time = (slots_per_frame - slots_per_user) * slot_duration;
            
            delay_analysis(user_idx, frame_idx) = avg_waiting_time;
        else
            delay_analysis(user_idx, frame_idx) = NaN;
        end
    end
end

%% 可视化结果
fig_tdma = figure('Name', 'TDMA多址接入技术分析', 'Position', [200, 100, 1200, 800]);

% 子图1: TDMA容量 vs SNR
subplot(2, 2, 1);
for user_idx = 1:num_user_cases
    plot(EbN0_dB, capacity_tdma(:, user_idx)/1e6, 'LineWidth', 2, 'Color', colors(user_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('容量 (Mbps)');
title('TDMA容量 vs 信噪比');
legend(arrayfun(@(x) sprintf('%d用户', x), num_users, 'UniformOutput', false), 'Location', 'NorthWest');

% 子图2: 吞吐量 vs 用户数量
subplot(2, 2, 2);
snr_index = round(num_snr/2);
plot(num_users, throughput(snr_index, :)/1e6, 'o-', 'LineWidth', 2, 'Color', colors(1, :));
grid on;
xlabel('用户数量');
ylabel('吞吐量 (Mbps)');
title(sprintf('TDMA吞吐量 vs 用户数量 (Eb/N0 = %d dB)', EbN0_dB(snr_index)));

% 子图3: 延迟分析
subplot(2, 2, 3);
for frame_idx = 1:num_frame_cases
    valid_users = find(~isnan(delay_analysis(:, frame_idx)));
    if ~isempty(valid_users)
        plot(num_users(valid_users), delay_analysis(valid_users, frame_idx)*1000, ...
             'o-', 'LineWidth', 2, 'Color', colors(frame_idx, :));
        hold on;
    end
end
grid on;
xlabel('用户数量');
ylabel('平均延迟 (ms)');
title('TDMA延迟分析');
legend(arrayfun(@(x) sprintf('%d时隙/帧', x), frame_structures, 'UniformOutput', false), 'Location', 'NorthEast');

% 子图4: 时隙分配效率
subplot(2, 2, 4);
efficiency = zeros(num_user_cases, num_frame_cases);
for user_idx = 1:num_user_cases
    K = num_users(user_idx);
    for frame_idx = 1:num_frame_cases
        slots_per_frame = frame_structures(frame_idx);
        slots_per_user = floor(slots_per_frame / K);
        efficiency(user_idx, frame_idx) = (slots_per_user * K) / slots_per_frame;
    end
end

% 绘制效率热图
imagesc(efficiency);
colorbar;
xlabel('帧结构 (时隙数量)');
ylabel('用户数量');
title('TDMA时隙分配效率');
set(gca, 'YTick', 1:num_user_cases, 'YTickLabel', num_users);
set(gca, 'XTick', 1:num_frame_cases, 'XTickLabel', frame_structures);

fprintf('TDMA分析完成！\n');

end